package test;
import java.util.Scanner;
public class qqq2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int t = scan.nextInt();
        int[][] a = new int[t][4];
        int[] b = new int[10000000];
        for (int i = 0; i < t; i++) {
            for (int j = 0; j < 4; j++) {
                a[i][j] = scan.nextInt();
            }
        }
        for (int i = 0; i < t; i++) {
            for (int k = 1; k <= a[i][3]; k++) {
                b[a[i][0] * k] = 1;
                b[a[i][1] * k] = 1;
                b[a[i][2] * k] = 1;
            }
            int q = 0;
            for (int k = 1; k <=100000; k++) {
                if (b[k] == 1) {
                    q += 1;
                    if (q == a[i][3]) {
                        System.out.println(k);
                        break;
                    }

                }
            }
            for (int k = 1; k <= 100000; k++) {
                b[k] = 0;
            }

        }
    }
}
